(function($) {

	$(function() {

		//增加黑名单
		$('#add-blacklist').dialog({
			autoOpen : false,
			resizable: false,
			height : 300,
			width : 420,
			modal : true,
			buttons : {
				'保存' : function() {
					if($('#add-blacklist-form').validate(VALIDATE.addBlackList).form()){
						if ($('#forbid').val() == 0) {
							showTips("请选择封禁时间");
							return;
						}
						var that = $(this);
						//ie不支持trim
						$.ajax({
							url : ctx+'/blacklist/add.do?v=20121012',
							data : {
								passport : $('#passport').val().replace(/(^\s*)|(\s*$)/g, ''),
								forbid : $('#forbid').val(),
								memo : $('#forbid option:selected').text()
							},
							success : function(ret) {
								switch(ret){
								case 'success':
									showTips('插入成功',function(){that.dialog('close');window.location.reload();});
									break;
								case 'existed':
									showTips('通行证"'+$('#passport').val().replace(/(^\s*)|(\s*$)/g, '')+'"已在黑名单中');
									break;
								case 'error':
									showTips('插入失败');
									break;
								}
							}
						});
					}
				},
				'取消' : function() {
					$(this).dialog('close');
				}
			}
		});
		$('.add-blacklist-link').click(function(e) {
			e.preventDefault();
			$('#add-blacklist').dialog('open');
		});
		
		//查询
		$('#s_btn').click(function(){
			var form = $('#s_form');
			$.browser.msie && clearPlaceholder(form);
			if (form.validate(VALIDATE.searchBlackList).form())
				form.submit();
		});
		
		//点击删除
		$('.to_delete').click(function(e) {
			var that = $(this);
			showTipsWithCancel("确定删除?", deleteBlackList(that));
		});
		
	});
	
	function deleteBlackList(that){
		//需要使用return的形式，否则作为参数时会先执行函数，然后将返回值作为参数，而不是函数本身
		return function(){$.ajax({
			url : ctx + "/blacklist/remove.do",
			data : {
				blacklistId : that.attr('id')
			},
			success : function(ret) {
				if (ret.startWith('error:')){
					showTips(ret.substring(6),function(){that.dialog('close');});
				}else{
					showTips("删除成功",function(){that.dialog('close');window.location.reload();});
				}
			}
		});}
	}
	
})(jQuery);
